package com.jqbd.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jqbd.common.MyJSON;
import com.jqbd.listener.UserDatalistener;
import com.jqbd.mapper.UserMapper;
import com.jqbd.pojo.User;
import com.jqbd.service.UserService;
import com.jqbd.vo.LacVo;
import com.jqbd.vo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Enzo Cotter on 2022/11/10.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public MyJSON<User> findAll(Map<String, Object> map) {
        int page = Integer.parseInt(map.get("page") + "");
        int limit = Integer.parseInt(map.get("limit") + "");
        System.out.println(page+"     "+limit);
        PageHelper.startPage(page, limit);
        List<User> userList = userMapper.findAll(map);
        for (User user : userList) {
            System.out.println(user);
        }
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        MyJSON<User> myJSON = new MyJSON<>();
        myJSON.setMsg("用户信息");
        myJSON.setCode(0);
        myJSON.setData(pageInfo.getList());
        myJSON.setCount(pageInfo.getTotal());
        return myJSON;
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public MyJSON<String> selectnickname() {
        List<String> selecttnickname = userMapper.selecttnickname();
        MyJSON<String> myJSON = new MyJSON<>();
        myJSON.setMsg("用户信息");
        myJSON.setCode(0);
        myJSON.setData(selecttnickname);
        myJSON.setCount((long) selecttnickname.size());
        return myJSON;
    }

//    @Override
//    public void insertList(List<UserData> cachedDataList) {
//
//        for (UserData userData : cachedDataList) {
//            int insert = userMapper.insert(userData);
//        }
//
//    }

    @Override
    public void  writeExcel(Integer[] ids, HttpServletResponse response) {
        //判断
        if(ids.length > 0){
            //查询数据库
            List<User> patientList = userMapper.findByIds(ids);
            //格式化当前时间
            String currTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

            //System.out.println("当前系统时间戳：" + System.currentTimeMillis());
            // 写法2
            //String fileName = "D:/patient-" + System.currentTimeMillis() + ".xlsx";
            //String fileName = "D:/patient-" + currTime + ".xlsx";
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            // 如果这里想使用03 则 传入excelType参数即可
            //EasyExcel.write(fileName, Patient.class).sheet("患者列表").doWrite(patientList);

            //通过下载的方式导出Excel文件
            String fileName = "patient-" + currTime + ".xlsx";
            //设置响应头
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
            //通过流的方式写入
            ServletOutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //导出Excel
            EasyExcel.write(outputStream, UserData.class).sheet("用户表").doWrite(patientList);
        }else {
            //如果没有选择性导出数据，那么就导出所有
            //查询数据库
            List<User> patientList = userMapper.findAllNoparm();
            //格式化当前时间
            String currTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

            //通过下载的方式导出Excel文件
            String fileName = "patient-" + currTime + ".xlsx";
            //设置响应头
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
            //通过流的方式写入
            ServletOutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //导出Excel
            EasyExcel.write(outputStream, UserData.class).sheet("用户数据").doWrite(patientList);
        }
    }

    @Override
    public void save(MultipartFile file) {
        InputStream inputStream = null;

        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //读取上传的Excel文件
        EasyExcel.read(inputStream, UserData.class,
                new UserDatalistener(userMapper)).sheet().doRead();

    }

    @Override
    public void insertList(List<UserData> cachedDataList) {
        userMapper.insertList(cachedDataList);
    }

    @Override
    public Map<Object, Object> stat() {
        List<LacVo> list =userMapper.stat();
        Map<Object, Object> map =new HashMap<>();
        map.put("data",list);
        return map;
    }

    @Override
    public int deleteList(String str) {
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
        }
        return userMapper.deleteList(list);
    }
}
